package com.hison.tandainbao.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hison.tandainbao.entiy.pojo.domain.TbBlog;
import com.hison.tandainbao.entiy.pojo.domain.TbShopInfo;
import com.hison.tandainbao.entiy.pojo.domain.TbShopType;
import com.hison.tandainbao.mapper.TbShopTypeMapper;
import com.hison.tandainbao.response.ResponseResult;
import com.hison.tandainbao.service.TbShopInfoService;
import com.hison.tandainbao.mapper.TbShopInfoMapper;
import com.hison.tandainbao.util.UserHolder;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResult;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author hison
 * @description 针对表【tb_shop_info】的数据库操作Service实现
 * @createDate 2024-04-01 21:09:22
 */
@Service
public class TbShopInfoServiceImpl extends ServiceImpl<TbShopInfoMapper, TbShopInfo>
        implements TbShopInfoService {

    @Autowired
    private TbShopInfoMapper mapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResponseResult queryById(Long id) {
        TbShopInfo shopInfo = mapper.selectById(id);
        if (shopInfo == null) {
            return ResponseResult.error("查询失败");
        }
        return ResponseResult.ok(shopInfo);
    }

    @Override
    public ResponseResult queryShopByType(Integer typeId, Integer current, Double x, Double y) {
        if (x == null || y == null) {  //如果坐标为null则查询数据库
            // 根据类型分页查询
            Page<TbShopInfo> page = query()
                    .eq("type_id", typeId)
                    .eq("status", 1)
                    .page(new Page<>(current, 10));
            // 返回数据
            return ResponseResult.ok(page.getRecords());

        }
        return ResponseResult.ok(Collections.emptyList());
    }

    @Override
    public ResponseResult saveShopInfo(TbShopInfo shop) {
        shop.setStatus(0);
        int i = mapper.insert(shop);
        if (i < 1) {
            return ResponseResult.error("新增失败");
        }
        return ResponseResult.ok("新增成功，待管理员审核");
    }

    @Override
    public ResponseResult getShopInfo(Integer current) {

        // 根据类型分页查询
        Page<TbShopInfo> page = query()
                .eq("status", 1)
                .orderByDesc("score")
                .page(new Page<>(current, 10));

        List<TbShopInfo> records = page.getRecords();

        return ResponseResult.ok(records);
    }

    @Override
    public ResponseResult adminUpdateStatus(Integer id, Integer status) {
        int i = mapper.adminUpdateStatus(id, status);
        if (i < 1) {
            return ResponseResult.error("审核失败");
        }

        return ResponseResult.ok("审核成功");
    }

    @Override
    public ResponseResult delete(String id) {
        int i = mapper.deleteById(id);
        if (i < 1) {
            return ResponseResult.error("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }

    @Override
    public ResponseResult getAllByAdmin(Integer current, Integer total) {
        // 根据用户查询
        Page<TbShopInfo> page = query()
                //根据状态升序排列
                .orderByAsc("status","create_time")
                //分页
                .page(new Page<>(current, total));
        //查询出当前页的博客信息
        List<TbShopInfo> records = page.getRecords();
        if (records.size() == 0) {
            return  ResponseResult.error("查询结果为空");
        }
        records.forEach(tbShopInfo -> {
            queryShopType(tbShopInfo);
        });
        return ResponseResult.ok(records);
    }

    @Autowired
    private TbShopTypeMapper  typeMapper;

    //获取店铺类型以及店铺图标
    private void queryShopType(TbShopInfo tbShopInfo) {
        //获取店铺id
        Long typeId= tbShopInfo.getTypeId();
        //根据店铺id获取对应的类型名称和类型图标
        TbShopType tbShopType = typeMapper.selectById(typeId);
        tbShopInfo.setShopType(tbShopType.getName());
        tbShopInfo.setShopIcon(tbShopType.getIcon());
    }


}




